#pragma once


//toate astea pentru IHTMLDocument2

#include <mshtml.h>
#include <mshtmhst.h>  //IDocHostUIHandler

#include "qpanel.h"
#include "qcom.h"


namespace micro
{
namespace browser
{


// This class implements an ActiveX control container
class CAXWindow:
    public IOleClientSite,
    public IOleInPlaceSite, public IOleInPlaceFrame,
    public IOleControlSite,  public IDocHostUIHandler,  public IDispatch
{
public:
    CAXWindow();
    virtual ~CAXWindow();
    void CreateControl( BSTR bstrClsid );
    void CreateControl( CLSID clsid );

    void Navigate( LPCTSTR str );
    void GoBack();
    void GoForward();
    void GoHome();
    void GoSearch();
    void Refresh();
    void Stop();

    void Remove();


    void Write( LPCTSTR string );
    void Clear();

    bool GetBusy() const;
    QString GetLocationName() const;
    QString GetLocationURL() const;
    IHTMLDocument2 * GetDocument();
    IWebBrowser2  * GetBrowser()
    {
        return m_pIWebBrowser2;
    }





    void SetParent( HWND hWndParent );
    void SetLocation( int x, int y, int width, int height );
    void SetVisible( BOOL fVisible );
    void SetFocus( BOOL fFocus );
    void SetStatusWindow( HWND hWndStatus );
    void TranslateKey( MSG msg );



    //IDispatch * GetDispatch();
    IUnknown * GetUnknown();

    // IUnknown Methods
    STDMETHODIMP QueryInterface( REFIID riid, void ** ppvObject );
    STDMETHODIMP_( ULONG ) AddRef();
    STDMETHODIMP_( ULONG ) Release();

    // IOleClientSite Methods
    STDMETHODIMP SaveObject();
    STDMETHODIMP GetMoniker( DWORD dwAssign, DWORD dwWhichMoniker, LPMONIKER * ppMk );
    STDMETHODIMP GetContainer( LPOLECONTAINER * ppContainer );
    STDMETHODIMP ShowObject();
    STDMETHODIMP OnShowWindow( BOOL fShow );
    STDMETHODIMP RequestNewObjectLayout();

    // IOleWindow Methods
    STDMETHODIMP GetWindow( HWND * phwnd );
    STDMETHODIMP ContextSensitiveHelp( BOOL fEnterMode );

    // IOleInPlaceSite Methods
    STDMETHODIMP CanInPlaceActivate();
    STDMETHODIMP OnInPlaceActivate();
    STDMETHODIMP OnUIActivate();
    STDMETHODIMP GetWindowContext( IOleInPlaceFrame ** ppFrame, IOleInPlaceUIWindow ** ppDoc, LPRECT lprcPosRect, LPRECT lprcClipRect, LPOLEINPLACEFRAMEINFO lpFrameInfo );
    STDMETHODIMP Scroll( SIZE scrollExtent );
    STDMETHODIMP OnUIDeactivate( BOOL fUndoable );
    STDMETHODIMP OnInPlaceDeactivate();
    STDMETHODIMP DiscardUndoState();
    STDMETHODIMP DeactivateAndUndo();
    STDMETHODIMP OnPosRectChange( LPCRECT lprcPosRect );

    // IOleInPlaceUIWindow Methods
    STDMETHODIMP GetBorder( LPRECT lprectBorder );
    STDMETHODIMP RequestBorderSpace( LPCBORDERWIDTHS lpborderwidths );
    STDMETHODIMP SetBorderSpace( LPCBORDERWIDTHS lpborderwidths );
    STDMETHODIMP SetActiveObject( IOleInPlaceActiveObject * pActiveObject, LPCOLESTR lpszObjName );

    // IOleInPlaceFrame Methods
    STDMETHODIMP InsertMenus( HMENU hmenuShared, LPOLEMENUGROUPWIDTHS lpMenuWidths );
    STDMETHODIMP SetMenu( HMENU hmenuShared, HOLEMENU holemenu, HWND hwndActiveObject );
    STDMETHODIMP RemoveMenus( HMENU hmenuShared );
    STDMETHODIMP SetStatusText( LPCOLESTR pszStatusText );
    STDMETHODIMP EnableModeless( BOOL fEnable );
    STDMETHODIMP TranslateAccelerator( LPMSG lpmsg, WORD wID );

    // IOleControlSite Methods
    STDMETHODIMP OnControlInfoChanged();
    STDMETHODIMP LockInPlaceActive( BOOL fLock );
    STDMETHODIMP GetExtendedControl( IDispatch ** ppDisp );
    STDMETHODIMP TransformCoords( POINTL * pptlHimetric, POINTF * pptfContainer, DWORD dwFlags );
    STDMETHODIMP TranslateAccelerator( LPMSG pMsg, DWORD grfModifiers );
    STDMETHODIMP OnFocus( BOOL fGotFocus );
    STDMETHODIMP ShowPropertyFrame();


    // IDocHostUIHandler pentru a suprima borderul3D
    STDMETHODIMP ShowContextMenu(
        DWORD dwID,
        POINT * ppt,
        IUnknown * pcmdtReserved,
        IDispatch * pdispReserved );

    STDMETHODIMP GetHostInfo( DOCHOSTUIINFO * pInfo );

    STDMETHODIMP ShowUI(
        DWORD dwID,
        IOleInPlaceActiveObject * pActiveObject,
        IOleCommandTarget * pCommandTarget,
        IOleInPlaceFrame * pFrame,
        IOleInPlaceUIWindow * pDoc ) ;

    STDMETHODIMP HideUI( void );
    STDMETHODIMP UpdateUI( void );

    //exista mai sus
    //STDMETHODIMP EnableModeless( BOOL fEnable ) ;
    STDMETHODIMP OnDocWindowActivate( BOOL fActivate );
    STDMETHODIMP OnFrameWindowActivate( BOOL fActivate );
    STDMETHODIMP ResizeBorder( LPCRECT prcBorder, IOleInPlaceUIWindow * pUIWindow, BOOL fRameWindow );
    STDMETHODIMP TranslateAccelerator( LPMSG lpMsg,  const GUID * pguidCmdGroup, DWORD nCmdID );
    STDMETHODIMP GetOptionKeyPath( LPOLESTR * pchKey, DWORD dw );
    STDMETHODIMP GetDropTarget( IDropTarget * pDropTarget, IDropTarget ** ppDropTarget );
    STDMETHODIMP GetExternal( IDispatch ** ppDispatch );
    STDMETHODIMP TranslateUrl( DWORD dwTranslate,  LPWSTR pchURLIn, LPWSTR * ppchURLOut );
    STDMETHODIMP FilterDataObject( IDataObject * pDO, IDataObject ** ppDORet );




    // IDispatch Methods
    STDMETHODIMP GetIDsOfNames( REFIID riid, OLECHAR ** rgszNames, unsigned int cNames, LCID lcid, DISPID * rgdispid );
    STDMETHODIMP GetTypeInfo( unsigned int itinfo, LCID lcid, ITypeInfo ** pptinfo );
    STDMETHODIMP GetTypeInfoCount( unsigned int * pctinfo );
    STDMETHODIMP Invoke( DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS * pdispparams, VARIANT * pvarResult, EXCEPINFO * pexecinfo, unsigned int * puArgErr );

private:
    ULONG       m_cRefs;       // ref count
    HWND        m_hWnd;        // window handle of the container
    HWND        m_hWndStatus;  // status window handle



    IUnknown	*	m_pUnk;				// IUnknown of contained object
    IWebBrowser2  * m_pIWebBrowser2;
    IOleObject	*	m_pWebObject;		// IOleObject of contained object


    QRect			m_rcControl;		// size of control
};





class QEventSink : public DWebBrowserEvents2  //IDispatch,
{
    ULONG       m_cRefs;		// ref count
public:
    QEventSink();

    // IUnknown Methods
    STDMETHODIMP QueryInterface( REFIID riid, void ** ppvObject );
    STDMETHODIMP_( ULONG ) AddRef();
    STDMETHODIMP_( ULONG ) Release();

    // IDispatch Methods
    STDMETHODIMP GetIDsOfNames( REFIID riid, OLECHAR ** rgszNames, unsigned int cNames, LCID lcid, DISPID * rgdispid );
    STDMETHODIMP GetTypeInfo( unsigned int itinfo, LCID lcid, ITypeInfo ** pptinfo );
    STDMETHODIMP GetTypeInfoCount( unsigned int * pctinfo );
    STDMETHODIMP Invoke( DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS * pdispparams, VARIANT * pvarResult, EXCEPINFO * pexecinfo, unsigned int * puArgErr );

};


///////////////////////////////////////////////
// CAXWindow to host the IWebBrower2 interface.
class QWebBrowser : public QPanel
{
protected:
    QString m_url;
public:
    QWebBrowser( const QString & url = L"" );
    virtual ~QWebBrowser();
    virtual bool Create( QWindow * pParent /* = nullptr */, int id, UINT style );

    void Navigate( LPCTSTR str );
    void GoBack();
    void GoForward();
    void GoHome();
    void GoSearch();
    void Refresh();
    void Stop();

    bool Print();

protected:
    virtual bool OnPreCreate( CREATESTRUCT & cs );
    virtual bool OnSize( int type, int cx, int cy );
    virtual bool OnDestroy();

    virtual bool OnPaint()
    {
        return true;
    }
    virtual bool OnEraseBackground( QGraphics * )
    {
        return true;
    }



protected:
    void Write( LPCTSTR string );
    void Clear();

    CAXWindow & GetAXWindow() const
    {
        return ( CAXWindow & ) m_AXContainer;
    }

    virtual QString GetText() const
    {
        return _T( "WebBrowser" );
    }


private:
    CAXWindow		m_AXContainer;		// The ActiveX Container

    QEventSink		m_EventSink;
    DWORD			m_eventCookie;



    DWORD	ConnectEvents(  REFIID iid, LPUNKNOWN punkSink );
    void	DisconnectSink( REFIID iid, DWORD dwCookie );

};




class CHTMLWriter
{
protected:
    IUnknown * m_pUnk;
    IWebBrowser2 * m_pWB;
    IDispatch * m_pDoc;
    bool m_bReleaseIWebBrowserPtr;

public:
    CHTMLWriter( IUnknown * pUnk );
    CHTMLWriter( IWebBrowser2 * pWB );
    ~CHTMLWriter( void );
    bool Write( LPCTSTR pszHTMLContent );
    bool Add( LPCTSTR pszHTMLContent );

private:
    CHTMLWriter( void ) {};
    bool GetDocumentPtr( void );
};







}
}//namespace
